package com.feeling.oafeeling.utils.util;

import com.feeling.oafeeling.pojo.SysUser;
import com.feeling.oafeeling.utils.util.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Configuration
public class RedisUtil {

    @Autowired
    @Qualifier("redisTemplate")
    RedisTemplate redisTemplate;

    @Autowired
    TokenService tokenService;

    public  void set(String s, SysUser userForBase, Long expired) {

        String token = tokenService.getToken(userForBase);
        redisTemplate.opsForValue().set(s,token,expired, TimeUnit.MINUTES);
    }

    public void set(Long id,SysUser user){
        redisTemplate.opsForValue().set(String.valueOf(id),user);
    }

    public  void del(String key) {

        if (key!=null){
            redisTemplate.delete(key);
        }


//        if (key != null && key.length() > 0) {
//            if (key.length() == 1) {
//                redisTemplate.delete(key[0]);
//            } else {
//                redisTemplate.delete(Arrays.asList(key));
////                redisTemplate.delete(CollectionUtils.arrayToList(key));
//            }
//        }
    }
    public void delAll(Set<String> keys){
        for (String k:keys) {
            del(k);
        }
    }

    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    public Set<String> keys(String s) {
        Set keys = redisTemplate.keys(s);
        return keys;
    }
}
